package client

import (
	"distributed_db/utils"
	"fmt"
	"strings"
)

type RequestData struct {
	TableName  string            `json:"table_name"`
	Cols       map[string]string `json:"cols"`
	Condition  string            `json:"condition"`
	TableNames []string          `json:"table_names"`
	Values     string            `json:"values"`
}

// 依据query构造请求
// query格式支持
// select cols from table_name where cond
// insert into table_name (cols) values (values)
// delete from table_name where cond
// create table table_name (cols)
// drop table table_name
// keepalive [table_names]
// keepalive用于保持连接

func (c *Client) constructRequest(query string) (Request, error) {
	// 删除query中的“(”“)”字符
	query = strings.ReplaceAll(query, "(", "")
	query = strings.ReplaceAll(query, ")", "")
	query = strings.ReplaceAll(query, ",", "")
	tokens := strings.Split(query, " ")
	switch tokens[0] {
	case "create":
		if len(tokens) < 4 {
			return Request{}, fmt.Errorf("missing table name and columns in create query: %s", query)
		}
		table := tokens[2]
		index := 3
		names := make(map[string]string, 0)
		for ; index < len(tokens)-1; index += 2 {
			name := tokens[index]
			typ := tokens[index+1]
			names[name] = typ
		}
		data := RequestData{TableName: table, Cols: names}
		utils.LogC(utils.Blue, "Constructed create request: %v\n", data)
		return Request{Type: "create", Data: data}, nil
	case "drop":
		if len(tokens) < 3 {
			return Request{}, fmt.Errorf("missing table name in drop query: %s", query)
		}
		table := tokens[2]
		data := RequestData{TableName: table}
		utils.LogC(utils.Blue, "Constructed drop request: %v\n", data)
		return Request{Type: "drop", Data: data}, nil
	case "select":
		cols := make(map[string]string, 0)
		i := 1
		for ; i < len(tokens)-2 && tokens[i] != "from"; i++ {
			cols[tokens[i]] = ""
		}
		if len(tokens) < 4 {
			return Request{}, fmt.Errorf("missing table name in select query: %s", query)
		}
		table := tokens[i+1]
		condition := ""
		if len(tokens) > 4 {
			condition = strings.Join(tokens[i+3:], " ")
		}
		data := RequestData{TableName: table, Cols: cols, Condition: condition}
		utils.LogC(utils.Blue, "Constructed select request: %v\n", data)
		return Request{Type: "select", Data: data}, nil
	case "insert":
		table := tokens[2]
		index1 := 3
		index2 := 3
		for tokens[index2] != "values" {
			index2++
		}
		index2++
		cols := make(map[string]string, 0)

		for index2 < len(tokens) {
			cols[tokens[index1]] = tokens[index2]
			index1++
			index2++
		}
		data := RequestData{
			TableName: table, Cols: cols}
		utils.LogC(utils.Blue, "Constructed insert request: %v\n", data)
		return Request{Type: "insert", Data: data}, nil

	case "delete":
		table := tokens[2]
		if len(tokens) < 4 {
			return Request{}, fmt.Errorf("missing condition in delete query: %s", query)
		}
		condition := strings.Join(tokens[4:], "")
		data := RequestData{TableName: table, Condition: condition}
		utils.LogC(utils.Blue, "Constructed delete request: %v\n", data)
		return Request{Type: "delete", Data: data}, nil
	case "keepalive":
		var tableNames []string
		if len(tokens) >= 1 {
			tableNames = tokens[1:]
		}
		data := RequestData{TableNames: tableNames}
		utils.LogC(utils.Blue, "Constructed keepalive request: %v\n", data)
		return Request{Type: "keepalive", Data: data}, nil

	default:
		return Request{}, fmt.Errorf("unsupported query: %s", query)
	}
}
